<?php

namespace app\login\controller;

use app\index\model\NewsModel;
use app\index\model\VisitedModel;
use app\index\service\WechatService;
use think\Cookie;
use think\Db;
use think\Exception;
use think\Jwt;
use think\Output;
use think\Request;
use think\Session;
use think\View;

class Base {
    private $wechatConfig = [
        "token"          => "d39070c2bb4e4ba105be8165269b1b37",
        "EncodingAESKey" => "8Qh6cADSJUWeUbkynnlWbFZxzkDWbneFeTsq54urDG5"
    ];

    protected $uid = '';

    public function __construct(Request $request) {
        //微信校验
        try {
            $this->checkSignature($request);
        } catch (Exception $e) {

        }
        //微信分享分享
       /* $wechatService = new WechatService();
        $weJSSDKConfig = $wechatService->getJSSDKInfo($request);
        View::share('weJSSDKConfig', $weJSSDKConfig);
        if ($_SERVER['HTTP_HOST'] != 'www.houannj.com') redirect('https://www.houannj.com');*/
        //启动日志
        Output::run();
        $menu = [
            ['name' => '首页', 'url' => '/index.html'],
            ['name' => '新闻资讯', 'url' => '/news.html'],
            ['name' => '热门景点', 'url' => '/scenery.html'],
            ['name' => '旅游攻略', 'url' => '/strategy.html'],
//            ['name' => '预订房间', 'url' => '/room.html'],
//            ['name' => '菜单明细', 'url' => '/food.html'],
        ];
        View::share('webConfig', [
            'title'       => '溪边居民宿-后岸农家乐69号',
            'description' => '溪边居农家乐-后岸农家乐69号',
            'keywords'    => '后岸,农家乐,溪边居,民宿,天台街头',
            'static_url'  => config('static_url'),
        ]);
        View::share('menu_list', $menu);
        View::share('description', '');
        View::share('title', '');
        View::share('keywords', '');
        $jwt = Jwt::getInstance();
        //添加访问日志
        $this->addVisitedLog($request);
        $this->uid = $jwt->get('uid');
    }

    /**
     * 添加日志
     * @param Request $request
     * @return bool
     */
    private function addVisitedLog(Request $request) {
        try {
            if (!in_array($request->server('REQUEST_URI'), ['/getUserInfo', '/Sidebar'])) {
                $data         = [
                    'token'      => Session::get('jwt_token'),
                    'ip'         => $request->ip(),
                    'url'        => $request->server('REQUEST_SCHEME') . '://' . $request->server('HTTP_HOST') . $request->server('REQUEST_URI'),
                    'referer'    => $request->server('HTTP_REFERER'),
                    'user_agent' => $request->server('HTTP_USER_AGENT'),
                    'add_time'   => time()
                ];
                $visitedModel = new VisitedModel();
                $visitedModel->create($data);
            }
        } catch (Exception $e) {

        }
        return true;
    }

    private function checkSignature(Request $request) {
        $echostr   = $request->get("echostr");
        $nonce     = $request->get("nonce");
        $timestamp = $request->get("timestamp");
        $signature = $request->get("signature");
        $array     = [$this->wechatConfig["token"], $timestamp, $nonce];
        sort($array);
        $str = implode($array);
        if ($signature == sha1($str)) {
            echo $echostr;
            exit;
        }
    }
}
